<#include "../common/layout/__sublayout.html">
    <#include "../common/__pagination.html">

        <!-- page specific plugin styles -->
        <!--设置面包屑-->
        <#assign pageBreadCrumbs>
            <li class="active"><a href="#">消息推送管理</a></li>
            <li class="active">编辑</li>
        </#assign>


        <#assign pageCss>
            <!-- page specific plugin styles -->
            <link rel="stylesheet" href="${ctx}/static/admin/assets/css/jquery-ui.custom.css"/>
            <link rel="stylesheet" href="${ctx}/static/admin/assets/css/chosen.css"/>
            <link rel="stylesheet" href="${ctx}/static/admin/assets/css/datepicker.css"/>
        </#assign>

        <#assign pageJavascript>
            <script src="${ctx}/static/admin/assets/js/jquery.dataTables.js"></script>
            <script src="${ctx}/static/admin/assets/js/jquery.dataTables.bootstrap.js"></script>
            <script src="${ctx}/static/admin/assets/js/ajaxfileupload.js"></script>
            <script src="${ctx}/static/admin/assets/js/bootstrap-wysiwyg.js"></script>

            <script type="text/javascript">
                var $path_assets = "${ctx}/static/admin/assets/";//this will be used in loading jQuery UI if needed!</script>
        <script type="text/javascript">
//            $(document).ready(function(){
//                showDiv = "";
//                generalType = "${sg.generalType!}";
//                if(generalType ==0) {
//                    showDiv = "generalWordDiv";
//                }else if(generalType ==1) {
//                    showDiv = "softDiv";
//                }else if(generalType ==2){
//                    showDiv = "topicDiv";
//                }else if(generalType ==4) {
//                    showDiv = "linkUrlDiv";
//                }
//                $("#" + showDiv).show();
//            });
        </script>

<script type="text/javascript">


</script>
</#assign>
<@sublayout pageBreadCrumbs=pageBreadCrumbs pageCss=pageCss pageJavascript=pageJavascript currentMenu="ApplicationFlag">


<form id="generalForm" class="form-horizontal" action="${ctx}/admin/general/save.do"  method="post">

    <div class="row">
        <div class="col-sm-12">
            <div class="widget-box">
                <div class="widget-header">
                    <h4 class="widget-title">推广基础信息</h4>

                </div>
                <div class="widget-body">
                    <div class="widget-main">
                        <div class="form-group">
                            <label class="control-label col-xs-12 col-sm-2 no-padding-right" for="id">推广编号:</label>
                            <div class="col-xs-12 col-sm-4">
                                <div class="clearfix">
                                    <input type="text" name="id" id="id" class="col-xs-12 " readonly="" value="${sg.id!}"/>
                                </div>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="control-label col-xs-12 col-sm-2 no-padding-right" for="id">开始时间:</label>
                            <div class="col-xs-12 col-sm-4">
                                <div class="clearfix">
                                    <div class="col-sm-4">
                                        <div class="input-medium input-group">
                                            <span style="position: relative; z-index: 9999;">
                                            <input type="text" name="beginDate" id="beginDate" class="input-medium date-picker required" value="${sg.endTimeShow!}" data-date-format="yyyy-mm-dd"/>
                                            </span>
                                            <span class="input-group-addon"> <i class="ace-icon fa fa-calendar"></i></span>
                                        </div>
                                    </div>
                                </div>
                            </div>

                        </div>

                        <div class="form-group">
                            <label class="control-label col-xs-12 col-sm-2 no-padding-right" for="id">结束时间:</label>
                            <div class="col-sm-4  ">
                                <div class="clearfix">
                                    <div class="col-sm-4">
                                        <div class="input-medium input-group">
                                            <span style="position: relative; z-index: 9999;">
                                            <input type="text" name="endDate" id="endDate" class="input-medium date-picker" value="${sg.endTimeShow!}" data-date-format="yyyy-mm-dd"/>
                                            </span>
                                            <span class="input-group-addon"> <i class="ace-icon fa fa-calendar"></i></span>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!----消息对象管理-------->
    <div class="row">
        <div class="col-sm-12">
            <div class="widget-box">
                <div class="widget-header">
                    <h4 class="widget-title">推广类型管理</h4>
                </div>

                <div class="widget-body">
                    <div class="widget-main">
                        <div class="form-group">
                            <label class="control-label col-xs-12 col-sm-2 no-padding-right" for="id">推广类型:</label>
                            <div class="col-sm-4  ">
                                <div class="clearfix">
                                    <select class="form-control" name="generalType" id="generalType" onchange="changeGeneralType(this)" data-placeholder="Choose a State...">
                                        <option value="-1">请选择类型</option>
                                        <option value="0">搜索关键字</option>
                                        <option value="1">软件</option>
                                        <option value="2">专题</option>
                                        <option value="4">外链</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <hr>
                <!----软件-------->
                <div id="softDiv" class="widget-body " hidden>
                    <div class="widget-main">
                        <div class="form-group">
                            <label class="control-label col-xs-12 col-sm-2 no-padding-right" for="id">应用编号:</label>
                            <div class="col-xs-12 col-sm-3">
                                <div class="clearfix">
                                    <input type="text" name="softId" id="softId" class="col-xs-12 " />
                                </div>
                            </div>
                            <label class="control-label col-xs-12 col-sm-1 no-padding-right" for="id">应用名称:</label>
                            <div class="col-sm-3  ">
                                <div class="clearfix">
                                    <input type="text" name="softName" id="softName" class="col-xs-12 " />
                                </div>
                            </div>
                            <input type="button" id="searchSoftButton" class="btn btn-sm btn-primary" onclick="resetSoft()" value="查询"/>&nbsp;
                        </div>
                    </div>
                    <div class="widget-main">
                        <table id="topicTable" class="table table-striped table-bordered table-hover">
                            <thead>
                            <tr>
                                <th width="40" align="center">请选择</th>
                                <th width="40" align="center">应用编号</th>
                                <th width="100" align="center">应用名称</th>
                            </tr>
                            </thead>
                            <tbody id="softtbody">
                            <#if checkSofts??>
                                <#list checkSofts as soft>
                                    <tr>
                                        <td  align="center">
                                            <div class="radio"><label> <input name="softradio" type="radio" class="ace" checked="checked" value="${soft.id!}"/> <span class="lbl"> </span></label></div>
                                        </td>
                                        <td  align="center">${soft.id!}</td>
                                        <td  align="center">${soft.softName!}</td>
                                    </tr>
                                </#list>
                            </#if>
                            </tbody>
                        </table>
                    </div>
                </div>

                <!----外部连接-------->
                <div id="linkUrlDiv" class="form-group" hidden>
                    <div class="widget-main">
                        <div class="frorm-group">
                            <label class="control-label col-xs-12 col-sm-2" for="linkUrl">外部链接:</label>
                            <div class="col-xs-12 col-sm-4">
                                <div class="clearfix">
                                    <input type="text" name="linkUrl" id="linkUrl" class="form-control col-sm-4" value="${sg.linkUrl!}"/>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!----专题-------->
                <div id="topicDiv" class="widget-body " hidden>
                    <div class="widget-main">
                        <div class="form-group">
                            <label class="control-label col-xs-12 col-sm-2 no-padding-right" for="id">专题编号:</label>
                            <div class="col-xs-12 col-sm-3">
                                <div class="clearfix">
                                    <input type="text" name="topicId" id="topicId" class="col-xs-12 "/>
                                </div>
                            </div>
                            <label class="control-label col-xs-12 col-sm-1 no-padding-right" for="id">专题名称:</label>
                            <div class="col-sm-3  ">
                                <div class="clearfix">
                                    <input type="text" name="topicName" id="topicName" class="col-xs-12 " />
                                </div>
                            </div>
                            <input type="button" id="searchTopicButton" class="btn btn-sm btn-primary" onclick="resetTopic()" value="查询"/>&nbsp;
                        </div>
                    </div>
                    <div class="widget-main">
                        <table id="categoryTable" class="table table-striped table-bordered table-hover">
                            <thead>
                            <tr>
                                <th width="40" align="center">请选择</th>
                                <th width="40" align="center">专题编号</th>
                                <th width="100" align="center">专题名称</th>
                            </tr>
                            </thead>
                            <tbody id="topictbody">
                            <#if topics??>
                                <#list topics.listData as topic>
                                    <tr>
                                        <td  align="center">
                                            <div class="radio"><label> <input name="topicradio" type="radio" class="ace" checked="checked" value="${topic.id!}"/> <span class="lbl"> </span></label></div>
                                        </td>
                                        <td  align="center">${topic.id!}</td>
                                        <td  align="center">${topic.topicName!}</td>
                                    </tr>
                                </#list>
                            </#if>
                            </tbody>
                        </table>
                    </div>
                </div>

                <!----推广文字-------->
                <div id="generalWordDiv" class="widget-main" hidden>
                    <div class="form-group">
                        <label class="control-label col-xs-12 col-sm-2" for="generalWord">推广文字:</label>
                        <div class="col-xs-12 col-sm-4">
                            <div class="clearfix">
                                <input type="text" name="generalWord" id="generalWord" class="form-control col-sm-4" value="${sg.generalWord!}"/>
                            </div>
                        </div>
                    </div>
                </div>

            </div>
        </div>
    </div>

    <div class="modal-footer center">
        <button type="button" class="btn btn-sm btn-success" onclick="saveGeneral()"><i class="ace-icon fa fa-check"></i> 保存</button>
        <button type="button" class="btn btn-sm" data-dismiss="modal"  onclick="cancel()"><i class="ace-icon fa fa-times"></i> 取消</button>
    </div>


</form>
    </@sublayout>
    <script src="${ctx}/static/admin/assets/js/jquery.validate.js"></script>
    <script src="${ctx}/static/admin/assets/js/jquery.validate.messages_zh.js"></script>
<script type="text/javascript">

    function cancel(){
        $("#generalForm")[0].reset();
        window.location="${refer!}";
    }

    function saveGeneral(){
        generalType = $("#generalType").val();
        if(generalType == 1){
            var softradio = $('input[name="softradio"]:checked').val();
            if(!softradio){
                alert("请选择软件");
                return false;
            }
        }else if(generalType == 0){
            generalWord = $("#generalWord").val();
            if(generalWord==""){
                alert("请填写推广文字");
            }
        }else if(generalType == 2){

        }else if(generalType == 4){
            linkUrl = $("#linkUrl").val();
            if(linkUrl==""){
                alert("请填写外部链接");
                return false;
            }
        }
        $("#generalForm").submit();

    }

    function changeGeneralType(node){
        generalType = $(node).val();
        showDiv = "generalWordDiv";
        if(generalType ==0) {
            showDiv = "generalWordDiv";
        }else if(generalType ==1) {
            resetSoft();
        }else if(generalType ==2){
            resetTopic();
        }else if(generalType ==4) {
            showDiv = "linkUrlDiv";
        }
        divs = new Array("generalWordDiv","linkUrlDiv","softDiv","topicDiv");
        for (index in divs) {
            if (divs[index] == showDiv) {
                $("#" + divs[index]).show();
            } else {
                $("#" + divs[index]).hide();
            }
        }
    }

    function resetSoft(){
        showDiv = "softDiv";
        var url="${ctx}/admin/soft/searchSoft.do";
        softId = $("#softId").val();
        softName = $("#softName").val();
        //将选中的一级下拉列表项的id传过去
        $.getJSON(url, {"id": softId,"softName":softName,"pageSize":20},function (data) {
            //对请求返回的JSON格式进行分解加载
            $("#softtbody").empty();
            $(data).each(function () {
                content = '<tr><td><div class="radio"><label> <input name="softradio" type="radio" value="'+this.id+'" class="ace" /> <span class="lbl"> </span></label></div></td>'
                +'<td>'+this.id+'</td>'
                +'<td>'+this.softName+'</td></tr>'
                $("#softtbody").append(content);
            });
        });
    }

    function resetTopic(){
        showDiv = "topicDiv";
        var url="${ctx}/admin/topic/getTopicList.do";
        topicId = $("#topicId").val();
        topicName = $("#topicName").val();
        //将选中的一级下拉列表项的id传过去
        $.getJSON(url, {"topicId": topicId,"topicName":topicName},function (data) {
            //对请求返回的JSON格式进行分解加载
            $("#topictbody").empty();
            $(data.listData).each(function () {
                content = '<tr><td><div class="radio"><label> <input name="topicradio" type="radio" value="'+this.id+'" class="ace" /> <span class="lbl"> </span></label></div></td>'
                +'<td>'+this.id+'</td>'
                +'<td>'+this.topicName+'</td></tr>';
                $("#topictbody").append(content);
            });
        });
    }


    $(document).ready(function(){
//        $("#beginDate").val(${sg.beginDate!});
//        $("#endDate").val(${sg.endDate!});
        $("#generalType").val(${sg.generalType!});
        generalType = $("#generalType").val();
        if(generalType ==1){
            $("#softDiv").show();
        }else if(generalType ==0){
            $("#generalWordDiv").show();
        }else if(generalType ==2){
            $("#topicDiv").show();
        }else if(generalType ==4){
            $("#linkUrlDiv").show();
        }

        $(".date-picker").datepicker({
            language : 'zh-CN',
            autoclose: true,
            todayHighlight: true
        });

        $("#generalForm").validate({
            errorElement: 'div',
            errorClass: 'help-block',
            focusInvalid: true,
            highlight: function (e) {
                $(e).closest('.form-group').removeClass('has-info').addClass('has-error');
            },
            success: function (e) {
                $(e).closest('.form-group').removeClass('has-error');//.addClass('has-info');
                $(e).remove();
            },
            errorPlacement: function (error, element) {
                if(element.is('input[type=checkbox]') || element.is('input[type=radio]')) {
                    var controls = element.closest('div[class*="col-"]');
                    if(controls.find(':checkbox,:radio').length > 1) controls.append(error);
                    else error.insertAfter(element.nextAll('.lbl:eq(0)').eq(0));
                }
                else if(element.is('.select2')) {
                    error.insertAfter(element.siblings('[class*="select2-container"]:eq(0)'));
                }
                else if(element.is('.chosen-select')) {
                    error.insertAfter(element.siblings('[class*="chosen-container"]:eq(0)'));
                }
                else error.insertAfter(element.parent());
            },

            submitHandler: function (form) {
                $.ajax({
                    cache: true,
                    type: "POST",
                    url:"${ctx}/admin/general/save.do",
                    data:$('#generalForm').serialize(),// 你的formid
                    async: false,
                    error: function(request) {
                        alert("Connection error");
                    },
                    success: function(data) {
                        if(data.result == "success"){
                            window.location="${refer!}";
                        }else{
                            alert(data.messages);
                        }
                    }
                });
            },
            invalidHandler: function (form) {
            }
        })
    });
</script>